import { createRouter, createWebHistory } from 'vue-router'
import UserMainPageView from "@/views/UserMainPageView.vue";
import UserInfoView from "@/views/user/UserInfoView.vue";
import UserHomeView from "@/views/user/UserHomeView.vue";
import LoginView from "@/views/LoginView.vue";
import AdministratorMainPageView from "@/views/AdministratorMainPageView.vue";
import AdministratorHomeView from "@/views/administrator/AdministratorHomeView.vue";
import AdministratorInfoView from "@/views/administrator/AdministratorInfoView.vue";
import AdministratorManagementView from "@/views/administrator/AdministratorManagementView.vue";
import UserActivityApplyView from "@/views/user/UserActivityApplyView.vue";
import AdministratorActivityView from "@/views/administrator/AdministratorActivityView.vue";
import UserActivityView from "@/views/user/UserActivityView.vue";
import UserActivityContent from "@/views/user/userActivity/UserActivityContent.vue";
import UserActivityGroup from "@/views/user/userActivity/UserActivityGroup.vue";
import {useUserStore} from "@/stores/user.js";
import {useAdministratorStore} from "@/stores/administrator.js";
import {currentUserInfo} from "@/api/user.js";
import {currentAdministratorInfo} from "@/api/administrator.js";
import UserGroup from "@/views/user/UserGroupView.vue";
import UserFeedbackView from "@/views/user/UserFeedbackView.vue";
import AdministratorActivityFeedbackView from "@/views/administrator/AdministratorActivityFeedbackView.vue";
import UserActivityMessageView from "@/views/user/UserActivityMessageView.vue";
import UserGroupMessageView from "@/views/user/UserGroupMessageView.vue";



const routes = [
  {
    path: '/',
    redirect: '/user', // 根路径重定向到 /user
  },
  {
    path: '/login',
    component: LoginView
  },
  {
    path: '/administrator',
    component: AdministratorMainPageView,
    // redirect: '/administrator/home',
    redirect: '/administrator/activity',
    children: [
      {
        path: 'home',
        name: 'administrator-home',
        component: AdministratorHomeView,
      },
      {
        path: 'info',
        name: 'administrator-info',
        component: AdministratorInfoView,
      },
      {
        path: 'management',
        name: 'administrator-management',
        component: AdministratorManagementView
      },
      {
        path: 'activity',
        name: 'administrator-activity',
        component: AdministratorActivityView
      },
      {
        path: 'feedback',
        name: 'administrator-activity-feedback',
        component: AdministratorActivityFeedbackView
      }
    ]

  },
  {
    path: '/user',
    component: UserMainPageView,
    // redirect: '/user/home', // /user 重定向到 /user/home
    redirect: '/user/activity', // /user 重定向到 /user/home
    children: [
      {
        path: 'home',
        name: 'home',
        component: UserHomeView,
      },
      {
        path: 'info',
        name: 'info',
        component: UserInfoView,
      },
      {
        path: 'activity/apply',
        name: 'activity_apply',
        component: UserActivityApplyView,
      },
      {
        path: 'message/activity',
        name: 'message_activity',
        component: UserActivityMessageView,
      },
      {
        path: 'message/group',
        name: 'message_group',
        component: UserGroupMessageView,
      },
      {
        path: 'activity',
        name: 'activity',
        component: UserActivityView,
        redirect: '/user/activity/content',
        children: [
          {
            path: 'content',
            name: 'activity_content',
            component: UserActivityContent,
          },
          {
            path: 'group',
            name: 'activity_group',
            component: UserActivityGroup,
          },
        ]
      },
      {
        path: 'group',
        name: 'group',
        component: UserGroup
      },
      {
        path: 'feedback',
        name: 'feedback',
        component: UserFeedbackView
      }
    ],
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});


router.beforeEach(async (to) => {
  if (to.path.startsWith('/user')) {
    const userStore = useUserStore()
    const res = await currentUserInfo(userStore);
    if (res.data.code !== 0) {
      return {path: '/login', query: { redirect: to.fullPath }}
    }
  }
  if (to.path.startsWith('/administrator')) {
    const administratorStore = useAdministratorStore()
    const res = await currentAdministratorInfo(administratorStore);
    if (res.data.code !== 0) {
      return {path: '/login'}
    }
  }
})


export default router
